/* 
 * keyboardInput.h
 *
 *  Created on: 11/02/2014
 *      Author: Emiliano R. Gudiño
 */

#ifndef KEYBOARDINPUT_H_
#define KEYBOARDINPUT_H_

#include <sys/un.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <string>
#include <stdlib.h>

#include <semaphore.h>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include "scpi/scpi.h"
#include "scpi/commands.h"
#include "scpi/responses.h"
#include "scpi/scpi_includes/scpi_def.h"
#include "Socket_Conn.h"

#define TYPE_SCPI_CMD 	'S'
#define TYPE_PARSED_CMD 'R'
#define DATA_OK		1
#define DATA_PENDING	0

// File names of the different sockets
#define FASTCGI_SOCKET_FILENAME "/tmp/fastcgi_socket"
#define TCP_SOCKET_FILENAME     "/tmp/tcp_socket"
#define TELNET_SOCKET_FILENAME  "/tmp/telnet_socket"
#define SERIAL_SOCKET_FILENAME  "/tmp/serial_socket"
#define LOCAL_SOCKET_TOTAL	4

#define MAX_PARAM_LENGTH	256

using namespace std;

class Keyboard_In
{
  public:
	Keyboard_In(sem_t * global_sem);										// Constructor
	virtual ~ Keyboard_In();												// Destructor
	bool Get_Command(command_request_str *);								// Read and clean the data in the buffer.
	void Set_Response(command_response_str *);								// Read and clean the data in the buffer.
	bool socket_open;

  private:
	  pthread_t thread;														// Socket listener thread
	command_request_str command_request;									// Commands buffer
	command_response_str command_response;									// Responses buffer
	char cmd_processed_flag;												// Flag that show if the data was processed
	sem_t buffer_queue_count;												// Semaphore that wait the process finish
	sem_t *global_queue_count;												// Semaphore that wait for a new command
	static void *Socket_Listener(void *);
	static void Scpi_Parser(char *, Keyboard_In *);							// Interpret the scpi commands
};

#endif /* KEYBOARDINPUT_H_ */
